//
//  MotionData.m
//  CoreMotionDemo
//
//  Created by 曹敬贺 on 16/9/14.
//  Copyright © 2016年 北京无限点乐科技有限公司. All rights reserved.
//

#import "MotionData.h"
#import <CoreGraphics/CoreGraphics.h>

/*
 CMMotionManager中包含三种数据，加速度，陀螺仪，磁场数据。对应accelerometer,gyro,magnetometer
 设置加速度的初始化属性（其他两个完全类似）
 setAccelerometerUpdateInterval
 */

@implementation MotionData


- (instancetype)init
{
    if (self = [super init]) {
        self.manager = [[CMMotionManager alloc]init];
    }
    return self;
}

//加速计
- (void)startAccelerometer
{
#pragma mark - 下面两个变量是粗略计步器用的，可忽略
    __block int stepCount = 0; // 步数
    __block BOOL valiadCountStep = YES;
    
    //刷新频率
    NSTimeInterval refreshTime = 0.06;
    /* 判断是否加速度传感器可用，如果可用则继续 */
    if ([self.manager isAccelerometerAvailable] == YES) {
        /* 给采样频率赋值，单位是秒 */
        [self.manager setAccelerometerUpdateInterval:refreshTime];
        /* 加速度传感器开始采样，每次采样结果在block中处理 */
        [self.manager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error)
         {
             NSLog(@"Data: %.2f  %.2f  %.2f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z);
//             //下面这种方法也是到主线程中去修改
//             [self performSelectorOnMainThread:@selector(updateView:) withObject:weakSelf waitUntilDone:NO];
             
             
             
             
#pragma mark - 粗略计步器
             CGFloat sqrtValue =sqrt(accelerometerData.acceleration.x*accelerometerData.acceleration.x+accelerometerData.acceleration.y*accelerometerData.acceleration.y+accelerometerData.acceleration.z*accelerometerData.acceleration.z);
             // 走路产生的震动率
             if (sqrtValue > 1.552188 && valiadCountStep)
             {
                 stepCount +=1;
                 NSLog(@"%d",stepCount);
                 valiadCountStep = NO;
             }else
             {
                 valiadCountStep = YES;
             }
             
         }];
    }
}
- (void)stopAccelerometer
{
    if ([self.manager isAccelerometerActive] == YES)
    {
        [self.manager stopAccelerometerUpdates];
    }
}
//陀螺仪
- (void)startGyro
{
    /* 设置采样的频率，单位是秒 */
    NSTimeInterval updateInterval = 1.f; // 每秒采样20次
    /* 判断是否陀螺仪传感器可用，如果可用则继续 */
    if ([self.manager isGyroAvailable] == YES) {
        /* 给采样频率赋值，单位是秒 */
        [self.manager setGyroUpdateInterval:updateInterval];
        [self.manager startGyroUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
            NSLog(@"Data：%.2f   %.2f   %.2f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z);
        }];
    }
}
- (void)stopGyro
{
    if ([self.manager isGyroActive] == YES)
    {
        [self.manager stopGyroUpdates];
    }
}
//磁力计
- (void)startMagnetometer
{
    /* 设置采样的频率，单位是秒 */
    NSTimeInterval updateInterval = 1.f; // 每秒采样20次
    //在block中，只能使用weakSelf。
    /* 判断是否磁力计传感器可用，如果可用则继续 */
    if ([self.manager isMagnetometerAvailable] == YES) {
        [self.manager setMagnetometerUpdateInterval:updateInterval];
        [self.manager startMagnetometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMMagnetometerData * _Nullable magnetometerData, NSError * _Nullable error) {
            NSLog(@"%.2f   %.2f   %.2f",magnetometerData.magneticField.x,magnetometerData.magneticField.y,magnetometerData.magneticField.z);
        }];
    }
}
- (void)stopMagnetpmeter
{
    if ([self.manager isMagnetometerActive] == YES)
    {
        [self.manager stopMagnetometerUpdates];
    }
}

- (void)startDeviceMotion
{
    NSLog(@"%@",[NSOperationQueue  currentQueue]);
    /* 设置采样的频率，单位是秒 */
    NSTimeInterval updateInterval = 1.f; // 每秒采样20次
    //在block中，只能使用weakSelf。
    /* 判断是否磁力计传感器可用，如果可用则继续 */
    if ([self.manager isDeviceMotionAvailable] == YES) {
        [self.manager setDeviceMotionUpdateInterval:updateInterval];
        [self.manager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
            NSLog(@"timestamp:%.2f",motion.timestamp);
            NSLog(@"%.2f , %.2f , %.2f",motion.attitude.roll,motion.attitude.pitch,motion.attitude.yaw);
            NSLog(@"%.2f , %.2f , %.2f , %.2f ,%.2f , %.2f , %.2f , %.2f , %.2f",motion.attitude.rotationMatrix.m11,motion.attitude.rotationMatrix.m12,motion.attitude.rotationMatrix.m13,motion.attitude.rotationMatrix.m21,motion.attitude.rotationMatrix.m22,motion.attitude.rotationMatrix.m23,motion.attitude.rotationMatrix.m31,motion.attitude.rotationMatrix.m32,motion.attitude.rotationMatrix.m33);
            NSLog(@"%.2f , %.2f , %.2f , %.2f",motion.attitude.quaternion.x,motion.attitude.quaternion.y,motion.attitude.quaternion.z,motion.attitude.quaternion.w);
            NSLog(@"-----------------------------------");
        }];
    }else
    {
        NSLog(@"不可用");
    }
}

@end
